use tauri::{App, Emitter};
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};

pub fn init_shortcuts(app: &mut App) -> tauri::Result<()> {
  let trigger_shortcut = Shortcut::new(Some(Modifiers::SUPER | Modifiers::SHIFT), Code::KeyX);
  let app_handle = app.handle().clone();

  app.handle().plugin(
    tauri_plugin_global_shortcut::Builder::new()
      .with_handler(move |_app, shortcut, event| {
        if shortcut == &trigger_shortcut {
          match event.state() {
            ShortcutState::Pressed => {
              app_handle.emit("trigger-shortcut", ()).unwrap();
            }
            ShortcutState::Released => {}
          }
        }
      })
      .build(),
  )?;

  let _ = app.handle().global_shortcut().register(trigger_shortcut);
  Ok(())
}
